package com.zmzncs.lmtc.module.promotion.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.common.pojo.PageInfo;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.promotion.pojo.ticket.*;
import com.zmzncs.lmtc.module.promotion.service.TicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-10-13
 */
@Slf4j
@Api(tags = "优惠券信息")
@RestController
@RequestMapping("/promotion/ticket")
public class TicketController {

    @Autowired
    private TicketService ticketService;


    @ApiOperation(value = "添加优惠券规则")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "addTicket")
    public Rest addTicket(@RequestBody @Validated TicketAddForm form) {
        ticketService.addTicket(form);
        return RestUtil.success();
    }

    @ApiOperation(value = "条件分页查询优惠券")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),})
    @PostMapping(value = "getTicketByManage")
    public Rest<IPage<TicketTableVO>> getTicketByManage(@RequestBody @Validated TicketSelectForm form) {
        IPage<TicketDTO> page = ticketService.getTicketByManage(form);
        PageInfo<TicketTableVO> pageInfo = JsonUtil.pageTransform(page, TicketTableVO.class);
        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "更新优惠券状态")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
            @ApiImplicitParam(paramType="query", name="ticketId", value="优惠券ID"),
            @ApiImplicitParam(paramType="query", name="status", value="状态(0：不可用 ，1：可用，2：删除)"),
    })
    @PostMapping(value = "updateTicketStatus")
    public Rest updateTicketStatus(@NotNull Long ticketId, @NotNull Integer status) {
        ticketService.updateTicketStatus(ticketId,status);
        return RestUtil.success();
    }

    @ApiOperation(value = "查询所有优惠券信息")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),})
    @PostMapping(value = "getAllTicket")
    public Rest<List<TicketTableVO>> getAllTicket() {
        List<TicketDTO> ticketDTOS = ticketService.getAllTicket();
        List<TicketTableVO> ticketList = JsonUtil.listTransform(ticketDTOS, TicketTableVO.class);
        return RestUtil.success(ticketList);
    }
}

